package com.thinkfit.jy.presenter;

import com.common.lib.bean.UserBean;
import com.common.lib.bean.VideoBean;
import com.common.lib.bean.VideoTagBean;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.BasePresenter;
import com.common.lib.network.HttpListener;
import com.common.lib.network.HttpMethods;
import com.common.lib.network.HttpObserver;
import com.thinkfit.jy.contract.VideoListContract;
import com.thinkfit.jy.utils.DeviceTypeUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

public class VideoListPresenter extends BasePresenter<VideoListContract.View> implements VideoListContract.Presenter {

    public VideoListPresenter(@NotNull VideoListContract.View rootView) {
        super(rootView);
    }

    @Override
    public void getVideoTag(){
        HttpMethods.Companion.getInstance().videoTag(DeviceTypeUtils.getDeviceType(),
                new HttpObserver(getRootView(), new HttpListener<ArrayList<VideoTagBean>>() {
                    @Override
                    public void onSuccess(@Nullable ArrayList<VideoTagBean> bean) {
                        getRootView().getVideoTagSuccess(bean);
                    }

                    @Override
                    public void dataError(int code, @androidx.annotation.Nullable String msg) {

                    }

                    @Override
                    public void connectError(@androidx.annotation.Nullable Throwable e) {

                    }
                }, getCompositeDisposable()));
    }

    public void getVideoCourse(int index,int minTime,int maxTime, int type,int page,int start,int vtagid){
        getVideoCourse(index,minTime,maxTime,type,page,start,vtagid,0);
    }

    public void getVideoCourse(int index,int minTime,int maxTime, int type,int page,int start,int vtagid,int isCollect){
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        HttpMethods.Companion.getInstance().videoList(
                myInfo == null ? "" : myInfo.getUid(),
                DeviceTypeUtils.getDeviceType(),type, minTime, maxTime,vtagid,start, isCollect,page,
                new HttpObserver(false, getRootView(), new HttpListener<ArrayList<VideoBean>>() {
                    @Override
                    public void onSuccess(@Nullable ArrayList<VideoBean> list) {
                        if (getRootView() == null) {
                            return;
                        }
                        if (page == 1) {
                            DataManager.Companion.getInstance().saveVideoList(type, index, list);
                        }
                        getRootView().getVideoCourseSuccess(page, list);
                    }

                    @Override
                    public void dataError(@Nullable int code, @Nullable String msg) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getVideoCourseFailed();
                    }

                    @Override
                    public void connectError(@Nullable Throwable e) {
                        if (getRootView() == null) {
                            return;
                        }
                        getRootView().getVideoCourseFailed();
                    }
                }, getCompositeDisposable()));
    }

//    @Override
//    public void getVideoCourse(int index, int type, int page) {
//        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  index : "+index+"   type : "+type+"   page : "+page);
//        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
//        HttpMethods.Companion.getInstance().videoList(
//                myInfo == null ? "" : myInfo.getUid(),
//                DeviceTypeUtils.getDeviceType(),type, index,0,0, page,
//                new HttpObserver(false, getRootView(), new HttpListener<ArrayList<VideoBean>>() {
//                    @Override
//                    public void onSuccess(@Nullable ArrayList<VideoBean> list) {
//                        if (getRootView() == null) {
//                            return;
//                        }
//                        if (page == 1) {
//                            DataManager.Companion.getInstance().saveVideoList(type, index, list);
//                        }
//                        getRootView().getVideoCourseSuccess(page, list);
//                    }
//
//                    @Override
//                    public void dataError(@Nullable int code, @Nullable String msg) {
//                        if (getRootView() == null) {
//                            return;
//                        }
//                        getRootView().getVideoCourseFailed();
//                    }
//
//                    @Override
//                    public void connectError(@Nullable Throwable e) {
//                        if (getRootView() == null) {
//                            return;
//                        }
//                        getRootView().getVideoCourseFailed();
//                    }
//                }, getCompositeDisposable()));
//    }
}
